package com.sihai.ehr.web.action.train;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.TrainAssessWeight;
import com.sihai.ehr.model.biz.TrainCategory;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.service.TrainAssessWeightService;
import com.sihai.ehr.service.TrainCategoryService;
import com.sihai.ehr.web.action.EHRAction;

/**
 * 培训类型控制器
 * 
 * @author henry
 * 
 */
@Results({ @Result(name = "main", location = "train_main.jsp"), 
		@Result(name = "assessweight_manage", location = "assessweight_manage.jsp"),
		@Result(name = "list", location = "assessweight_list.jsp") })
public class TrainAssessWeightAction extends EHRAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Log log = LogFactory.getLog(TrainAssessWeightAction.class);

	@BeanInject
	private OrganizationService organizationService;
	@BeanInject
	private TrainAssessWeightService trainAssessWeightService;

	private List<Organization> organizations;
	private LimitedList<TrainAssessWeight> assessWeights;
	private int assessType;

	/** 教育训练评价权重 */
	@Action("/train/assessweight_main")
	@Override
	public String execute() throws Exception {
		listTitle = "教育训练评价权重";
		listAction = "/train/assessweight_list.xhtml";
		organizations = organizationService.findChildren(null, true, null, null);
		if (organizations != null && organizations.size() > 0) {
			if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId() != topOrgId){
            			it.remove();
            		}
            	}
            }
			organizationId = organizations.get(0).getId();
		}
		showChildOrg = false;
		return "main";
	}

	@Action("/train/assessweight_list")
	public String list() {
		listAction = "/train/assessweight_list.xhtml";
		assessWeights = trainAssessWeightService.getTrainAssessWeights(organizationId, -1L, assessType, createLimit(), createSorter());
		if (assessWeights == null || assessWeights.size() <= 0) {
			initialWeightData();
			assessWeights = trainAssessWeightService.getTrainAssessWeights(organizationId, -1L, assessType, createLimit(), createSorter());
		}
		paging(assessWeights);
		/* 设置翻页action */
		this.setPageUrl("train/assessweight_list.xhtml");
		return "list";
	}

	/** 初始化权重数据 */
	private void initialWeightData() {
		TrainAssessWeight assessWeight = new TrainAssessWeight();
		// assessWeight.setOrganization(organizationService.get(organizationId));
		// assessWeight.setDuty(null);
		assessWeight.setTitle("心得报告");
		assessWeight.setPercent(60F);
		assessWeight.setAssessType(0);
		trainAssessWeightService.save(assessWeight);

		TrainAssessWeight assessWeight1 = new TrainAssessWeight();
		// assessWeight1.setOrganization(organizationService.get(organizationId));
		// assessWeight.setDuty(null);
		assessWeight1.setTitle("教师自评");
		assessWeight1.setPercent(20F);
		assessWeight1.setAssessType(0);
		trainAssessWeightService.save(assessWeight1);

		TrainAssessWeight assessWeight2 = new TrainAssessWeight();
		// assessWeight2.setOrganization(organizationService.get(organizationId));
		// assessWeight.setDuty(null);
		assessWeight2.setTitle("HR评价");
		assessWeight2.setPercent(20F);
		assessWeight2.setAssessType(0);
		trainAssessWeightService.save(assessWeight2);

		/*************** 外训 ****************/
		// 心得报告,课程资料下载,外训意见回馈单,发票或收据复印件,结业证书,出差报告单
		TrainAssessWeight assessWeight3 = new TrainAssessWeight();
		// assessWeight3.setOrganization(organizationService.get(organizationId));
		// assessWeight.setDuty(null);
		assessWeight3.setTitle("心得报告");
		// assessWeight3.setPercent(20F);
		assessWeight3.setAssessType(1);
		trainAssessWeightService.save(assessWeight3);

		TrainAssessWeight assessWeight4 = new TrainAssessWeight();
		// assessWeight2.setOrganization(organizationService.get(organizationId));
		// assessWeight.setDuty(null);
		assessWeight4.setTitle("外训意见回馈单");
		// assessWeight2.setPercent(20F);
		assessWeight4.setAssessType(1);
		trainAssessWeightService.save(assessWeight4);

		TrainAssessWeight assessWeight5 = new TrainAssessWeight();
		// assessWeight2.setOrganization(organizationService.get(organizationId));
		// assessWeight.setDuty(null);
		assessWeight5.setTitle("发票或收据复印件");
		// assessWeight5.setPercent(20F);
		assessWeight5.setAssessType(1);
		trainAssessWeightService.save(assessWeight5);

		TrainAssessWeight assessWeight6 = new TrainAssessWeight();
		// assessWeight2.setOrganization(organizationService.get(organizationId));
		// assessWeight.setDuty(null);
		assessWeight6.setTitle("结业证书");
		// assessWeight6.setPercent(20F);
		assessWeight6.setAssessType(1);
		trainAssessWeightService.save(assessWeight6);

		TrainAssessWeight assessWeight7 = new TrainAssessWeight();
		// assessWeight2.setOrganization(organizationService.get(organizationId));
		// assessWeight.setDuty(null);
		assessWeight7.setTitle("出差报告单");
		// assessWeight7.setPercent(20F);
		assessWeight7.setAssessType(1);
		trainAssessWeightService.save(assessWeight7);

	}

	public void setOrganizations(List<Organization> organizations) {
		this.organizations = organizations;
	}

	public List<Organization> getOrganizations() {
		return organizations;
	}

	public LimitedList<TrainAssessWeight> getAssessWeights() {
		return assessWeights;
	}

	public void setAssessWeights(LimitedList<TrainAssessWeight> assessWeights) {
		this.assessWeights = assessWeights;
	}

	public int getAssessType() {
		return assessType;
	}

	public void setAssessType(int assessType) {
		this.assessType = assessType;
	}
}
